import mongoose from "mongoose"

/* 评论模型 */
const reviewSchema = mongoose.Schema({
  userName: {
    type: String,
    required: true,
  },
  rating: {
    type: Number,
    required: true,
  },
  comment: {
    type: String,
    required: true,
  },
}, {
  timestamps: true
})

/* 产品模型 */
const productSchema = mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId, // 其他模型的id
    required: true,
    ref: 'User', // 关联用户
  },
  name: {
    type: String,
    required: true,
  },
  image: {
    type: String,
    required: true
  },
  brand: {
    type: String,
    required: true
  },
  category: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  rating: {
    type: Number,
    required: true
  },
  reviews: [reviewSchema],
  numReviews: {
    type: Number,
    required: true,
    default: 0
  },
  price: {
    type: Number,
    required: true
  },
  countInStock: {
    type: Number,
    required: true,
    default: 0
  },
}, {
  timestamps: true, // 更新时更新时间
})

const Product = mongoose.model("Product", productSchema)

export default Product